﻿using Dapper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;

namespace Respository.Base
{
    public class RepositoryBase<TEntity> : IRepositoryBase<TEntity> where TEntity : class,new()
    {
        public List<TEntity> QuerySP(string SPName)
        {
            using (IDbConnection Conn = SqlServerRespository.SqlServerContext.GetSqlConnection())
            {
                return Conn.Query<TEntity>(SPName, null, null, true, 0, CommandType.StoredProcedure).AsList();
            }
        }

        public List<TEntity> QuerySP(string SPName, object parms)
        {
            using (IDbConnection Conn = SqlServerRespository.SqlServerContext.GetSqlConnection())
            {
                return Conn.Query<TEntity>(SPName, parms, null, true, 0, CommandType.StoredProcedure).AsList();
            }
        }

        public async Task<List<TEntity>> QuerySPAsync(string SPName)
        {
            using (IDbConnection Conn = SqlServerRespository.SqlServerContext.GetSqlConnection())
            {
                // return await Task.Run(() => conn.Query<T>(SPName, null, null, true, null, CommandType.StoredProcedure).ToList());
                return (await  Conn.QueryAsync<TEntity>(SPName, null, null,0, CommandType.StoredProcedure)).AsList();
            }
        }

        public async Task<List<TEntity>> QuerySPAsync(string SPName, object parms)
        {
            using (IDbConnection Conn = SqlServerRespository.SqlServerContext.GetSqlConnection())
            {
               return (await Conn.QueryAsync<TEntity>(SPName, parms, null, 0, CommandType.StoredProcedure)).AsList();
            }
        }
    }
}
